#include "dateset.h"
#include "global.h"

#include <QDialog>
#include <QLineEdit>
#include <QDateEdit>
#include <QListWidget>
#include <QListWidgetItem>
#include <QHeaderView>
#include <QCalendarWidget>
#include <QTableWidgetItem>

int CURRENT_YEAR = 0; //  选择的年    导出时用
int CURRENT_MONTH = 0; // 选择的月    导出时用
int MINI_WORKDAY_DAY = 2;
int MINI_WORKDAY_NIGHT = 2;
int MINI_HOLIDAY_DAY = 2;
int MINI_HOLIDAY_NIGHT = 2;
int MINI_SPECIALDAY_DAY = 2;
int MINI_SPECIALDAY_NIGHT = 2;

// 日历中行数的初始值
#define ROW_COUNT   (4)

QList<QString > yearList = {"2022", "2023", "2024"," 2025", "2026", "2027", "2028", "2029", "2030"};
QList<QString> monthList = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"};

void getYearAndMonth(int & year, int & month);
void getIndexFromDate(int year, int month,int & yearIndex, int & monthIndex);
void getDateFromIndex(int yearIndex, int monthIndex,int & year, int & month);

void getYearAndMonth(int & year, int & month)
{
    QDate currentdate = QDate::currentDate();
    QString date = currentdate.toString("yyyy-MM-dd");
    year =  date.split("-").at(0).toInt();
    month = date.split("-").at(1).toInt();

    // 得到下一个排班日期的年月
    month+=1;
    if(month >= 13){
        month = 1;
        year+=1;
    }

    return;
}

void getIndexFromDate(int year, int month,int & yearIndex, int & monthIndex)
{
    for(int i = 0; i < yearList.count(); i++){
        if(year == yearList.at(i).toInt()){
            yearIndex = i;
            break;
        }
    }

    for(int i = 0; i < monthList.count(); i++){
        if(month == monthList.at(i).toInt()){
            monthIndex = i;
            break;
        }
    }

    return;
}

void getDateFromIndex(int yearIndex, int monthIndex,int & year, int & month)
{
    year = yearList.at(yearIndex).toInt();
    month = monthList.at(monthIndex).toInt();
    CURRENT_YEAR = year;
    CURRENT_MONTH = month;
    return;
}

DateSet::DateSet(QWidget *parent)
    : QWidget{parent}
{

}

DateSet::DateSet(QWidget *parent, QStackedWidget * pStackWidget)
{
    initPrivateVariable();

    setParent(parent);
    this->myParent = parent;
    this->pStackWidget = pStackWidget;

    // 绘制日期表格，不填充数据
    initDateTable();

    // 初始化界面下方的按钮
    initDateSetPushButton();

    return;
}

void DateSet::initPrivateVariable(void)
{
    this->bScheduleEnable = false;
    this->pStackWidget = NULL;
    this->settingWidget = NULL;
    this->pTable = NULL;
    this->pGetScheduleMonthDialog = NULL;
    this->pChangeWorkActionDialog = NULL;
    this->pDate = NULL;
    this->pChangeWorkActionDialogList = NULL;
    this->tableLayout = NULL;
    this->memberCountSettingLayout = NULL;
    this->tableAndLabelLayout = NULL;
    this->btnLayout = NULL;
    this->widgetLayout = NULL;
    this->iCurrentDialogYearComboBoxIndex = 0;
    this->iCurrentDialogMonthComboBoxIndex = 0;

    int iYear = 0, iMonth = 0;
    getYearAndMonth(iYear, iMonth);
    getIndexFromDate(iYear, iMonth, iCurrentDialogYearComboBoxIndex, iCurrentDialogMonthComboBoxIndex);

    return;
}

void DateSet::initDateTable(void)
{
    // 创建表格
    if(NULL != pTable){
        QLOG_WARN() <<"NULL != this->pTableWidget";
        return;
    }

    pTable = new MyDateTable(myParent);

    // 创建表头
    QStringList colLabels = {"周一", "周二", "周三", "周四", "周五", "周六", "周日"};
    pTable->createHorizontalHeader(colLabels);
    pTable->setRowCount(ROW_COUNT);

    // 设置样式
    pTable->setDateHorizontalHeaderStyle(QFont("song", 18), TABLEWIDGET_HORIZONTALHEADER_STYLE, true);
    pTable->setDateTableWidgetStyle();

    // 创建各单元格item
    for(int i = 0; i < pTable->rowCount(); i++){
        for (int j  = 0; j < pTable->columnCount(); j++){
            pTable->createItemAndSetStyle(i, j);
        }
    }

    // 创建右键菜单，监听菜单点击信号
    pTable->createActions();
    connect(pTable, &MyDateTable::signal_changeDayAction_triggered, this, &DateSet::slot_changeDayAction_triggered);
    connect(pTable, &MyDateTable::signal_changeWorkAction_triggered, this, &DateSet::slot_changeWorkAction_triggered);

    createSettingWidget();

    // 设置布局
    setTableAndLineLayout();

    return;
}

template<class Type>
void DateSet::deleteBoxLayout(Type *t)
{
    if(NULL == t)
        return;

    QLayoutItem* child;
    while((child = t->takeAt(0)) != 0){
        if(NULL == child->widget())
            continue;
        //必须将parent置为nullpptr，否则不消失
        t->removeWidget(child->widget());
        child->widget()->setParent(NULL);
        delete child;
    }

    return;
}

void DateSet::createSettingWidget()
{
    if(NULL != settingWidget)   delete settingWidget;
    settingWidget = new QWidget(myParent);

    QLabel * label1 = createSettingLabel("工作日白班人数", QFont("Microsoft YaHei", 14));
    QLabel * label2 = createSettingLabel("工作日夜班人数", QFont("Microsoft YaHei", 14));
    QLabel * label3 = createSettingLabel("节假日白班人数", QFont("Microsoft YaHei", 14));
    QLabel * label4 = createSettingLabel("节假日夜班人数", QFont("Microsoft YaHei", 14));
    QLabel * label5 = createSettingLabel("统一监控白班人数", QFont("Microsoft YaHei", 14));
    QLabel * label6 = createSettingLabel("统一监控夜班人数", QFont("Microsoft YaHei", 14));

    MyCombobox *combo1 = createSettingComboBox( QFont("Microsoft YaHei", 14), 1);
    MyCombobox *combo2 = createSettingComboBox( QFont("Microsoft YaHei", 14), 2);
    MyCombobox *combo3 = createSettingComboBox( QFont("Microsoft YaHei", 14), 3);
    MyCombobox *combo4 = createSettingComboBox( QFont("Microsoft YaHei", 14), 4);
    MyCombobox *combo5 = createSettingComboBox( QFont("Microsoft YaHei", 14), 5);
    MyCombobox *combo6 = createSettingComboBox( QFont("Microsoft YaHei", 14), 6);

    // 设置界面布局
    if(NULL != memberCountSettingLayout) delete memberCountSettingLayout;
    memberCountSettingLayout = new QGridLayout ;

    memberCountSettingLayout->addItem(new QSpacerItem(40, 20,QSizePolicy::Expanding), 0, 0);
    memberCountSettingLayout->addItem(new QSpacerItem(40, 20,QSizePolicy::Expanding), 0, 1);
    memberCountSettingLayout->addItem(new QSpacerItem(20, 150,QSizePolicy::Minimum, QSizePolicy::Fixed), 0, 2);
    memberCountSettingLayout->addItem(new QSpacerItem(40, 20,QSizePolicy::Expanding), 0, 4);
    memberCountSettingLayout->addItem(new QSpacerItem(40, 20,QSizePolicy::Expanding), 0, 7);
    memberCountSettingLayout->addItem(new QSpacerItem(40, 20,QSizePolicy::Expanding), 0, 8);
    memberCountSettingLayout->setColumnMinimumWidth(3, 100);
    memberCountSettingLayout->setColumnMinimumWidth(6, 100);
    memberCountSettingLayout->addWidget(label1, 1, 2);
    memberCountSettingLayout->addWidget(combo1, 1, 3);
    memberCountSettingLayout->addWidget(label2, 1, 5);
    memberCountSettingLayout->addWidget(combo2, 1, 6);
    memberCountSettingLayout->addWidget(label3, 2, 2);
    memberCountSettingLayout->addWidget(combo3, 2, 3);
    memberCountSettingLayout->addWidget(label4, 2, 5);
    memberCountSettingLayout->addWidget(combo4, 2, 6);
    memberCountSettingLayout->addWidget(label5, 3, 2);
    memberCountSettingLayout->addWidget(combo5, 3, 3);
    memberCountSettingLayout->addWidget(label6, 3, 5);
    memberCountSettingLayout->addWidget(combo6, 3, 6);
    memberCountSettingLayout->addItem(new QSpacerItem(20, 90,QSizePolicy::Minimum, QSizePolicy::MinimumExpanding), 4, 2);

    return;
}

QLabel *DateSet::createSettingLabel(QString text, QFont font)
{
    QLabel * label = new QLabel(text, myParent);
    label->setFont(font);
    return label;
}

MyCombobox *DateSet::createSettingComboBox(QFont font, int flag)
{
    MyCombobox *combo = new MyCombobox(myParent);
    QStringList list = {"2", "3", "4"};
    combo->setComboBoxStyle(combo, font, list, TABLEWIDGET_MEMBER_COMBOBOX_STYLE);

    if(1 == flag)
        connect(combo, QOverload<int>::of(&MyCombobox::currentIndexChanged), this, &DateSet::slot_settingCombobox1_changed);
    else if(2 == flag)
        connect(combo, QOverload<int>::of(&MyCombobox::currentIndexChanged), this, &DateSet::slot_settingCombobox2_changed);
    else if(3 == flag)
        connect(combo, QOverload<int>::of(&MyCombobox::currentIndexChanged), this, &DateSet::slot_settingCombobox3_changed);
    else if(4 == flag)
        connect(combo, QOverload<int>::of(&MyCombobox::currentIndexChanged), this, &DateSet::slot_settingCombobox4_changed);
    else if(5 == flag)
        connect(combo, QOverload<int>::of(&MyCombobox::currentIndexChanged), this, &DateSet::slot_settingCombobox5_changed);
    else if(6 == flag)
        connect(combo, QOverload<int>::of(&MyCombobox::currentIndexChanged), this, &DateSet::slot_settingCombobox6_changed);

    return combo;
}

void DateSet::setTableAndLineLayout()
{
    // table布局
    if(NULL != tableLayout) {
        deleteBoxLayout(tableLayout);
        delete tableLayout;
    }
    tableLayout = new QVBoxLayout;
    tableLayout->addWidget(pTable);

    // 总布局
    tableAndLabelLayout = new QVBoxLayout;
    tableAndLabelLayout->addLayout(tableLayout);
    tableAndLabelLayout->addLayout(memberCountSettingLayout);

    return;
}


void DateSet::getScheduleMonth(void)
{
    // 弹出对话框
    if(NULL == pGetScheduleMonthDialog){
        pGetScheduleMonthDialog = new QDialog(myParent);
        if(NULL == pGetScheduleMonthDialog){
            QLOG_WARN() <<"NULL == pGetScheduleMonthDialog";
            return;
        }
    } else{
        pGetScheduleMonthDialog->exec();
        return;
    }

    // 初始化对话框
    initDatesetDialog(pGetScheduleMonthDialog, QSize(300, 250), "选择排班月份");

    // 显示对话框中的控件
    // 2个combobox，年和月
    // 监听各个信号
    initDialogComboBox(pGetScheduleMonthDialog);

    // 两个按钮，确认和取消
    // 确认按钮：将选择的年月返回给上层
    // 取消按钮：关闭对话框，回到welcome界面
    // 监听各个信号
    initDialogPushbutton(pGetScheduleMonthDialog);

    // 以模态对话框形式运行
    pGetScheduleMonthDialog->exec();

    return;
}

void DateSet::connectDialogComboboxSignal(MyCombobox * pYear, MyCombobox * pMonth)
{
    if(NULL == pYear || NULL == pMonth){
        QLOG_WARN() << "NULL == pYear || NULL == pMonth";
        return;
    }

    connect(pYear, QOverload<int>::of(&MyCombobox::currentIndexChanged), this, [=](int index){
        iCurrentDialogYearComboBoxIndex = index;
    });

    connect(pMonth, QOverload<int>::of(&MyCombobox::currentIndexChanged), this, [=](int index){
        iCurrentDialogMonthComboBoxIndex = index;
    });

    return;
}

void DateSet::connectDialogPushButtonSignal(QPushButton * pYes, QPushButton * pCancel)
{
    if(NULL == pYes || NULL == pCancel){
        QLOG_WARN() << "NULL == pYes || NULL == pCancel";
        return;
    }

    connect(pYes, &QPushButton::clicked, this, [=](){
        bScheduleEnable = true;
        paintCalenderInDateTable(iCurrentDialogYearComboBoxIndex, iCurrentDialogMonthComboBoxIndex);
        pGetScheduleMonthDialog->close();
    });

    connect(pCancel, &QPushButton::clicked, this, [=](){
        pGetScheduleMonthDialog->close();
    });

    return;
}

void DateSet::paintCalenderInDateTable(int yearIndex, int monthIndex)
{
    int year = 0, month = 0;
    QList<myDay*> &mydayList = MYDAYLIST;

    // 清除表格中原有数据
    pTable->clearCalender();
    // 释放myDayList中的内容
    deinitMyDayList(mydayList);

    getDateFromIndex(yearIndex, monthIndex, year, month);
    CalcPosFromTheDay(mydayList, year, month);

    for(int i = 0; i < mydayList.count(); i++){
        myDay *pmyday = mydayList.at(i);
        pTable->paintCalender(pmyday->GetRow(), pmyday->GetCol(), QString::number(pmyday->GetDayOfMonth() + 1));
    }

    return;
}

void DateSet::deinitMyDayList(QList<myDay*> & mydayList)
{
    for(int i = 0; i < mydayList.count(); i++){
        if(NULL != mydayList.at(i))
            delete mydayList.at(i);
    }

    mydayList.clear();
    return;
}

void DateSet::CalcPosFromTheDay(QList<myDay*> & mydayList, int year, int month)
{
    int row = 0, col = 0;
    myDay * pMyDay = NULL;

    if( NULL ==  pDate){
        pDate = new QDate(year, month, 1);
        if(NULL == pDate){
            QLOG_WARN() << "NULL == pDate";
            return;
        }
    }
    else{
        pDate->setDate(year, month, 1);
    }

    for(int i = 0; i < pDate->daysInMonth(); i++){
        pMyDay = new myDay();
        if(NULL == pMyDay){
            QLOG_WARN() << "NULL == pMyDay";
            break;
        }

        pDate->setDate(year, month, i + 1);
        pMyDay->setMonth(month);
        pMyDay->SetDayOfMonth(pDate->day() - 1);
        pMyDay->SetDayOfWeek(pDate->dayOfWeek());
        if(pMyDay->GetDayOfWeek() >= 6 && pMyDay->GetDayOfWeek() <= 7)
            pMyDay->SetDayType(DATESET_HOLIDAY);
        else
            pMyDay->SetDayType(DATESET_WORKDAY);

        if(0 == i){
            col  = pMyDay->GetDayOfWeek() - 1;
            pMyDay->SetRow(row);
            pMyDay->SetCol(col);
        } else{
            col += 1;
            if(col >= 7){
                col -=7;
                row +=1;
            }
            pMyDay->SetRow(row);
            pMyDay->SetCol(col);
        }

        // 如果当天是节假日，并且存在有成员不上节假日的需求，那么在这里添加到当日不上班的容器里
#ifdef REQUIREMENT_ENABLE
        if(DATESET_HOLIDAY == pMyDay->GetDayType())
            setMemberNoHolidayWork(pMyDay);
#endif  // REQUIREMENT_ENABLE

        mydayList.append(pMyDay);
    }

    return;
}

void DateSet::setMemberNoHolidayWork(myDay *&pDay)
{
    if(NULL == pDay){
        QLOG_WARN() << "NULL == pDay";
        return;
    }

    MemberClass* pmember = NULL;

    for(int i = 0; i < MEMBERLIST.count(); i++){
        pmember = MEMBERLIST.at(i);
        if(NULL == pmember) continue;

        if(MEMBER_REQUIREMENT_NOHOLIDAY == pmember->getMemberRequirementIndex()){
            pDay->stringlistAppend(DATESET_NODAYWORK, pmember->getMemberName());
            pDay->stringlistAppend(DATESET_NONIGHTWORK, pmember->getMemberName());
            QLOG_INFO() << pmember->getMemberName() << "不上" << pDay->GetDayOfMonth() + 1 << "日的白班夜班。";
        }
    }

    return;
}

void DateSet::initDatesetDialog(QDialog *pDialog, QSize size, QString title)
{
    if(NULL == pDialog){
        QLOG_WARN() << "NULL == Dialog";
        return;
    }

    pDialog->setFixedSize(size);
    pDialog->setWindowTitle(title);
    pDialog->setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);

    return;
}

void DateSet::initDialogComboBox(QDialog *pDialog)
{
    if(NULL == pDialog){
        QLOG_WARN() << "NULL == Dialog";
        return;
    }

    MyCombobox * pComboYear = createDialogComboBox(pDialog, QPoint(50, 50), QSize(80, 35), yearList);
    if(NULL == pComboYear){
        QLOG_WARN() << "NULL == pComboYear";
        return;
    }

    MyCombobox * pComboMonth = createDialogComboBox(pDialog, QPoint(150, 50), QSize(80, 35), monthList);
    if(NULL == pComboMonth){
        QLOG_WARN() << "NULL == pComboMonth";
        return;
    }

    // ”年“ 和  “月” 的标签
    createDialogLabel(pDialog, QPoint(130, 50), QSize(40, 35), "年", QFont("Microsoft YaHei", 14));
    createDialogLabel(pDialog, QPoint(230, 50), QSize(40, 35), "月", QFont("Microsoft YaHei", 14));

    // 根据当前月份初始化index
    pComboYear->setCurrentIndex(iCurrentDialogYearComboBoxIndex);
    pComboMonth->setCurrentIndex(iCurrentDialogMonthComboBoxIndex);

    connectDialogComboboxSignal(pComboYear, pComboMonth);

    return;
}

void DateSet::initDialogPushbutton(QDialog *pDialog)
{
    if(NULL == pDialog){
        QLOG_WARN() << "NULL == Dialog";
        return;
    }

    QPushButton * pButtonYes = createDialogPushButton(pDialog, QPoint(35, 180), QSize(100, 35), "确定");
    if(NULL == pButtonYes){
        QLOG_WARN() << "NULL == pButtonYes";
        return;
    }

    QPushButton * pButtonCancel = createDialogPushButton(pDialog, QPoint(165, 180), QSize(100, 35), "取消");
    if(NULL == pButtonCancel){
        QLOG_WARN() << "NULL == pButtonCancel";
        return;
    }

    // 监听点击事件
    connectDialogPushButtonSignal(pButtonYes, pButtonCancel);
    return;
}

MyCombobox *  DateSet::createDialogComboBox(QDialog *pDialog, QPoint point, QSize size, QList<QString> stringlist)
{
    if(NULL == pDialog){
        QLOG_WARN() << "NULL == Dialog";
        return NULL;
    }

    MyCombobox * pComboYear = new MyCombobox(pDialog);
    pComboYear->move(point);
    pComboYear->setFixedSize(size);
    pComboYear->setComboBoxStyle(pComboYear, QFont("Microsoft YaHei", 14), stringlist, DATESET_DIALOG_COMBOBOX_STYLE);

    return pComboYear;
}

void DateSet::createDialogLabel(QDialog *pDialog, QPoint point, QSize size, QString text, QFont font)
{
    if(NULL == pDialog){
        QLOG_WARN() << "NULL == Dialog";
        return;
    }

    QLabel *pLabelYear = new QLabel(text, pDialog);
    pLabelYear->move(point);
    pLabelYear->setFixedSize(size);
    pLabelYear->setFont(font);

    return;
}

QPushButton * DateSet::createDialogPushButton(QDialog *pDialog, QPoint point, QSize size,QString text)
{
    if(NULL == pDialog){
        QLOG_WARN() << "NULL == Dialog";
        return NULL;
    }

    QPushButton * pButton = new QPushButton(text, pDialog);
    pButton->move(point);
    pButton->setFixedSize(size);
    pButton->setStyleSheet(DATESET_DIALOG_PUSHBUTTON_STYLE);

    return pButton;
}

void DateSet::initDateSetPushButton(void)
{
    int iHorizontalSpace = 350;
    QPushButton *pBtnRechoose = new QPushButton("重新选择日期", myParent);
    QPushButton *pBtnConfirm = new QPushButton("生成排班表", myParent);
    QPushButton *pBtnReturn = new QPushButton("上一步", myParent);

    pBtnRechoose->resize(150, 30);
    pBtnConfirm->resize(150, 30);
    pBtnReturn->resize(150, 30);

    pBtnRechoose->setStyleSheet(TABLEWIDGET_PUSHBUTTON_STYLE);
    pBtnConfirm->setStyleSheet(TABLEWIDGET_PUSHBUTTON_STYLE);
    pBtnReturn->setStyleSheet(TABLEWIDGET_PUSHBUTTON_STYLE);

    // 设置位置
    pBtnRechoose->move(20, pTable->height() + 10);
    pBtnConfirm->move(20 + iHorizontalSpace * 1.5, pTable->height() + 10);
    pBtnReturn->move(20 + iHorizontalSpace * 2, pTable->height() + 10);

    // 设置监听函数
    connect(pBtnRechoose, &QPushButton::clicked, this, &DateSet::slot_btnRechoose_clicked);
    connect(pBtnConfirm, &QPushButton::clicked, this, &DateSet::slot_btnConfirm_clicked);
    connect(pBtnReturn, &QPushButton::clicked, this, &DateSet::slot_btnReturn_clicked);

    // 设置布局
    if(NULL != btnLayout){
        deleteBoxLayout(btnLayout);
        delete btnLayout;
    }
    btnLayout = new QHBoxLayout;
    btnLayout->addStretch(1);
    btnLayout->addWidget(pBtnRechoose);
    btnLayout->addStretch(6);
    btnLayout->addWidget(pBtnConfirm);
    btnLayout->addStretch(1);
    btnLayout->addWidget(pBtnReturn);
    btnLayout->addStretch(1);

    //设置总体布局
    if(NULL != widgetLayout){
        deleteBoxLayout(widgetLayout);
        delete widgetLayout;
    }
    if(NULL != tableAndLabelLayout && NULL != btnLayout && NULL == widgetLayout){
        widgetLayout = new QVBoxLayout;
        widgetLayout->addLayout(tableAndLabelLayout);
        widgetLayout->addLayout(btnLayout);
        myParent->setLayout(widgetLayout);
    }

    return;
}

void DateSet::changeMyDayType(int row, int col, int dayType)
{
    QString indexstring;
    int index = 0;
    QList<myDay*> & mydayList = MYDAYLIST;

    if (0 != pTable->getDateItemText(row, col, indexstring)){
        QLOG_WARN() << "getDateItemText err";
        return;
    }

    if("" == indexstring){
        QLOG_WARN() << """ == indexstring";
        return;
    }

    index = indexstring.toInt();
    mydayList.at(index - 1)->SetDayType(dayType);
    // 如果当天是节假日，并且存在有成员不上节假日的需求，那么在这里添加到当日不上班的容器里
#ifdef REQUIREMENT_ENABLE
    if(DATESET_HOLIDAY == dayType)
        setMemberNoHolidayWork(mydayList[index - 1]);
#endif  // REQUIREMENT_ENABLE

    return;
}

void DateSet::changeWorkActionDialog(void)
{
    if(NULL == pChangeWorkActionDialog){
        pChangeWorkActionDialog = new QDialog(myParent);
        if(NULL == pChangeWorkActionDialog){
            QLOG_WARN() <<"NULL == pChangeWorkActionDialog";
            return;
        }
        initDatesetDialog(pChangeWorkActionDialog, QSize(300, 450), "选择人员");
        initDatesetWorkActionDialogPushButton(pChangeWorkActionDialog);
    } else{
        pChangeWorkActionDialog->show();
    }

    initDatesetWorkActionDialogListWidget(pChangeWorkActionDialog, pChangeWorkActionDialogList);

    // 以模态对话框形式运行
    pChangeWorkActionDialog->exec();
    return;
}

void DateSet::initDatesetWorkActionDialogPushButton(QDialog *pDialog)
{
    if( NULL == pDialog){
        QLOG_WARN() << "NULL == pDialog";
        return;
    }

    QPushButton * pButtonYes = createDialogPushButton(pChangeWorkActionDialog, QPoint(35, 410), QSize(100, 35), "确定");
    if(NULL == pButtonYes){
        QLOG_WARN() << "NULL == pButtonYes";
        return;
    }

    QPushButton * pButtonCancel = createDialogPushButton(pChangeWorkActionDialog, QPoint(165, 410), QSize(100, 35), "取消");
    if(NULL == pButtonCancel){
        QLOG_WARN() << "NULL == pButtonCancel";
        return;
    }
    // 连接按钮点击信号
    connect(pButtonYes, &QPushButton::clicked, this, [=](){
        slot_WorkActionDialogYesPushButton_clicked(pChangeWorkActionDialogList, pWorkActionDialogTriggeredDay);
        pChangeWorkActionDialog->close();
    });

    connect(pButtonCancel, &QPushButton::clicked, this, [=](){
        pChangeWorkActionDialog->close();
    });


    return;
}

void DateSet::initDatesetWorkActionDialogListWidget(QDialog *pDialog, QListWidget * &pList)
{
    if( NULL == pDialog){
        QLOG_WARN() << "NULL == pDialog";
        return;
    }

    QListWidgetItem *item = NULL;


    if(NULL != pList){
        int count = pList->count();
        for(int i = 0; i < count; i++){
             item = pList->takeItem(0);
             delete item;
        }
    } else{
        pList = new QListWidget(pDialog);
        pList->setFixedSize(QSize(300, 408));
    }

    reloadDatesetWorkActionDialogListWidget(pList, MEMBERLIST);

    return;
}

void DateSet::reloadDatesetWorkActionDialogListWidget(QListWidget *pList, QList<MemberClass*> memberlist)
{
    myDay* pMyDay = pWorkActionDialogTriggeredDay;
    if( NULL == pList ||  NULL == pMyDay){
        QLOG_WARN() << "NULL == pList ||  NULL == pMyDay";
        return;
    }

    QCheckBox *pCheck = NULL;
    int count = memberlist.count();
    QStringList namelist = pMyDay->getMemberList(pMyDay->getWorkType());

    for(int i = 0; i < count; i++){
        // 默认不勾选
        pCheck = createDatesetWorkActionDialogListWidgetCheckbox(pList, memberlist.at(i)->getMemberName());

        for(int j = 0; j < namelist.count(); j++){
            // 如果同名，符合条件，设为勾选
            // 先判断名字，名字不同跳过循环继续下一个；名字相同，直接跳出循环，因为姓名唯一
            if(memberlist.at(i)->getMemberName() != namelist.at(j))
                continue;
            pCheck->setCheckState(Qt::Checked);
            break;
        }
    }

    return;
}

QCheckBox *DateSet::createDatesetWorkActionDialogListWidgetCheckbox(QListWidget *pList, QString name)
{
    if( NULL == pList){
        QLOG_WARN() << "NULL == pList";
        return NULL;
    }

    QListWidgetItem *item = new QListWidgetItem();
    item->setSizeHint(QSize(10,35));
    QCheckBox * checkbox = new QCheckBox;
    checkbox->setText(name);
    checkbox->setFont(QFont("Microsoft YaHei", 12));
    checkbox->setTristate(false);
    checkbox->setStyleSheet(DATESET_WORKACTIONDIALOG_CHECKBOX_STYLE);
    pList->addItem(item);
    pList->setItemWidget(item, checkbox);

    return checkbox;
}

void DateSet::slot_changeDayAction_triggered(QPoint pos, int dayType)
{
    int row = 0, col = 0;
    if( NULL == pTable->getItemRowAndColFromPos(pos, row, col)){
        QLOG_WARN() << "getItemRowAndColFromPos err";
        return;
    }

    // 改变单元格的背景颜色
    pTable->changeDateItemBackground(row, col, dayType);

    // 改变mydaylist中指定list的daytype
    changeMyDayType(row, col, dayType);

    return;
}

void DateSet::slot_changeWorkAction_triggered(QPoint pos, int type)
{
    int index = 0;
    int row = 0, col = 0;
    QString indexstring;
    QList<myDay*> &mydayList = MYDAYLIST;

    if(NULL == pTable->getItemRowAndColFromPos(pos, row, col)){
        QLOG_WARN() << "getItemRowAndColFromPos err";
        return;
    }

    if (0 != pTable->getDateItemText(row, col, indexstring)){
        QLOG_WARN() << "getDateItemText err";
        return;
    }

    if("" == indexstring){
        QLOG_WARN() << """ == indexstring";
        return;
    }

    index = indexstring.toInt() - 1;
    pWorkActionDialogTriggeredDay = mydayList.at(index);
    if(NULL != pWorkActionDialogTriggeredDay)
        pWorkActionDialogTriggeredDay->setWorkType(type);

    // 弹出dialog，选定人员
    changeWorkActionDialog();

    return;
}

void DateSet::slot_WorkActionDialogYesPushButton_clicked(QListWidget *pList, myDay *pMyDay)
{
    if( NULL == pList ||  NULL == pMyDay){
        QLOG_WARN() << "NULL == pList ||  NULL == pMyDay";
        return;
    }

    QListWidgetItem *item = NULL;
    QCheckBox * check = NULL;
    QStringList namelist;

    for(int i = 0; i < pList->count(); i++){
        item = pList->item(i);
        if( NULL == item){
            QLOG_DEBUG() << "NULL == item";
            continue;
        }

        check = (QCheckBox *)pList->itemWidget(item);
        if( NULL == check){
            QLOG_DEBUG() << "NULL == check";
            continue;
        }

        if(Qt::Unchecked == check->checkState())
            continue;

        namelist.append(check->text());
    }
    pMyDay->setMemberList(namelist, pMyDay->getWorkType());

    return;
}

void DateSet::slot_settingCombobox1_changed(int index)
{
     MINI_WORKDAY_DAY = index + 2;
    return;
}

void DateSet::slot_settingCombobox2_changed(int index)
{
    MINI_WORKDAY_NIGHT = index + 2;
    return;
}

void DateSet::slot_settingCombobox3_changed(int index)
{
    MINI_HOLIDAY_DAY = index + 2;
    return;
}

void DateSet::slot_settingCombobox4_changed(int index)
{
    MINI_HOLIDAY_NIGHT = index + 2;
    return;
}

void DateSet::slot_settingCombobox5_changed(int index)
{
    MINI_SPECIALDAY_DAY = index + 2;
    return;
}

void DateSet::slot_settingCombobox6_changed(int index)
{
    MINI_SPECIALDAY_NIGHT = index + 2;
    return;
}

void DateSet::slot_dateSet_start(void)
{
    pStackWidget->setCurrentIndex(STACKWIDGET_DATE_INDEX);

    // 弹出对话框，获取所需排班的月份
    getScheduleMonth();

    return;
}

void DateSet::slot_btnRechoose_clicked(void)
{
    pGetScheduleMonthDialog->show();
    return;
}

void DateSet::slot_btnConfirm_clicked(void)
{
    if(true == bScheduleEnable)
        pStackWidget->setCurrentIndex(STACKWIDGET_SCHEDULE_INDEX);
    return;
}

void DateSet::slot_btnReturn_clicked(void)
{
    // 返回到上一界面
    pStackWidget->setCurrentIndex(STACKWIDGET_DATE_INDEX - 1);

    return;
}
